package com.taoyeshu.modules.sys.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.taoyeshu.common.annotation.SystemControllerLog;
import com.taoyeshu.common.utils.PageResult;
import com.taoyeshu.common.web.BaseController;
import com.taoyeshu.modules.sys.entity.Role;
import com.taoyeshu.modules.sys.entity.User;
import com.taoyeshu.modules.sys.query.UserQuery;
import com.taoyeshu.modules.sys.service.RoleService;
import com.taoyeshu.modules.sys.service.UserService;
import com.taoyeshu.modules.sys.vo.RoleVo;

@Controller
@RequestMapping("/sys/user/")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private RoleService roleService;

	@ModelAttribute
	public void getUser(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			map.put("user", userService.getById(id));
		}
	}

	@RequestMapping("list")
	public String list() {
		return "modules/sys/user/list";
	}

	@ResponseBody
	@RequiresPermissions("sys:user:view")
	@RequestMapping("listData")
	@SystemControllerLog(description = "查看用户")
	public PageResult<User> listData(UserQuery userQuery) {
		PageResult<User> result = userService.findPageResult(userQuery);
		return result;
	}

	@RequiresPermissions("sys:user:add")
	@RequestMapping("addView")
	public String addView(Model model) {
		List<Role> allRoles = roleService.queryAll();
		model.addAttribute("allRoles", allRoles);
		return "modules/sys/user/add";
	}

	@ResponseBody
	@RequiresPermissions("sys:user:add")
	@RequestMapping("add")
	@SystemControllerLog(description = "新增用户")
	public JSONObject add(User user, ArrayList<Integer> roleIds) {
		JSONObject json = new JSONObject();
		int id = userService.save(user);
		userService.saveUR(id, roleIds);
		if (id != 0) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	@RequiresPermissions("sys:user:update")
	@RequestMapping("updateView")
	public String updateView(int id, Model model, User user) {
		List<RoleVo> allRoleVos = roleService.queryAllRoleVos(user.getId());
		model.addAttribute("allRoleVos", allRoleVos);
		model.addAttribute("user", user);
		return "modules/sys/user/update";
	}

	@ResponseBody
	@RequiresPermissions("sys:user:update")
	@RequestMapping("update")
	@SystemControllerLog(description = "修改用户")
	public JSONObject update(User user, @RequestParam("roleIds") ArrayList<Integer> roleIds) {
		JSONObject json = new JSONObject();
		try {
			userService.removeUR(user.getId());
			userService.saveUR(user.getId(), roleIds);
			userService.update(user);
			json.put("code", 200);
			json.put("message", "success");
		} catch (Exception e) {
			System.out.println(e);
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	@ResponseBody
	@RequiresPermissions("sys:user:delete")
	@RequestMapping("delete")
	@SystemControllerLog(description = "删除用户")
	public JSONObject delete(User user) {
		JSONObject json = new JSONObject();
		try {
			userService.delete(user);
			json.put("code", 200);
			json.put("message", "success");
		} catch (Exception e) {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}
}
